// JavaScript Document
// 算法基类
// 在算法控制区域添加控件的函数
function addLabelToAlgorithmBar(labelName)
{
  const element = document.createTextNode(labelName);

  const tableEntry = document.createElement("td");
  tableEntry.appendChild(element);

  const controlBar = document.getElementById("AlgorithmSpecificControls");

  controlBar.appendChild(tableEntry);
  return element;
}
/* changed */
// select 标签
function addSelectToAlgorithmBar(optionList) {
  const element = document.createElement("select");
  for (let i=0; i<optionList.length; i++) {
    element.options.add(new Option (optionList[i], optionList[i]) );
  }

  const controlBar = document.getElementById("AlgorithmSpecificControls");

  controlBar.appendChild(element);
  return element;
}

// checkbox 勾选框
function addCheckboxToAlgorithmBar(boxLabel)
{
  const element = document.createElement("input");

  element.setAttribute("type", "checkbox");
  element.setAttribute("value", boxLabel);

  const label = document.createTextNode(boxLabel);

  const tableEntry = document.createElement("td");
  tableEntry.appendChild(element);
  tableEntry.appendChild(label);

  const controlBar = document.getElementById("AlgorithmSpecificControls");

  //Append the element in page (in span).
  controlBar.appendChild(tableEntry);
  return element;
}

// radio button 单选
function addRadioButtonGroupToAlgorithmBar(buttonNames, groupName)
{
  const buttonList = [];
  const newTable = document.createElement("table");

  for (let i = 0; i < buttonNames.length; i++)
  {
    const midLevel = document.createElement("tr");
    const bottomLevel = document.createElement("td");

    const button = document.createElement("input");
    button.setAttribute("type", "radio");
    button.setAttribute("name", groupName);
    button.setAttribute("value", buttonNames[i]);
    bottomLevel.appendChild(button);
    midLevel.appendChild(bottomLevel);
    const txtNode = document.createTextNode(` ${buttonNames[i]}`);
    bottomLevel.appendChild(txtNode);
    newTable.appendChild(midLevel);
    buttonList.push(button);
  }

  const topLevelTableEntry = document.createElement("td");
  topLevelTableEntry.appendChild(newTable);

  const controlBar = document.getElementById("AlgorithmSpecificControls");
  controlBar.appendChild(topLevelTableEntry);

  return buttonList
}


function addInputToAlgorithmBar(type, name) {

  const element = document.createElement("input");

  element.setAttribute("type", type);
  element.setAttribute("value", name);
  element.setAttribute("name", name);
  if (type == "text") {
    element.setAttribute("size","5");
  }

  const tableEntry = document.createElement("td");

  tableEntry.appendChild(element);


  const controlBar = document.getElementById("AlgorithmSpecificControls");

  //Append the element in page (in span).
  controlBar.appendChild(tableEntry);
  return element;

}

const Algorithm = () => {
};

// 初始化
Algorithm.prototype.init = function(animManager, width, height) {
  this.animationManager = animManager ; // 动画
  this.canvasWidth = width ; // 画布宽度
  this.canvasHeight = height ; // 画布高度
  this.commands = [] ; // 命令行
}

// 完成动作
Algorithm.prototype.implementAction = function(func, val) {
  const retVal = func(val);
  this.animationManager.startNewAnimation(retVal);
}

// commands生成函数
Algorithm.prototype.cmd = function(...args) {
  let command = args[0];
  for(i = 1; i < args.length; i++)
  {
    command = `${command}<cry>${String(args[i])}`;
  }
  this.commands.push(command);
}
